﻿Imports HMSLibrary
Imports HMSDataLayer
Imports System.Collections.ObjectModel

Public Class PatientTreatmentWindow
    Implements IHMSData

    Private Sub MasterWindow_Loaded(ByVal sender As Object, ByVal e As System.Windows.RoutedEventArgs) Handles Me.Loaded
        AddNew()
    End Sub

    Private Sub MainWindow_MouseDown(ByVal sender As Object, ByVal e As System.Windows.Input.MouseButtonEventArgs) Handles Me.MouseDown
        DragMove()
    End Sub

    Private Sub CloseButton_Click(ByVal sender As Object, ByVal e As System.Windows.RoutedEventArgs) Handles CloseButton.Click
        Me.Close()
    End Sub

    Private Sub MinimizeButton_Click(ByVal sender As Object, ByVal e As System.Windows.RoutedEventArgs) Handles MinimizeButton.Click
        Me.WindowState = Windows.WindowState.Minimized
    End Sub

    Public Property FormType As FormsList = FormsList.None

    Public Property ActionMode As HMSLibrary.FormAction Implements HMSLibrary.IHMSData.ActionMode

    Dim lstMed As New List(Of MedicineItemsDtl)

    Public Sub AddNew() Implements HMSLibrary.IHMSData.AddNew
        ActionMode = FormAction.ADD
        Dim dataProvider As New MasterDataLayer()
        Dim lst As List(Of Lookups)
        lst = dataProvider.GetPrescriptionTemplate()
        TemplateComboBox.ItemsSource = lst

        lst = dataProvider.GetLookupList(LookupTypes.MedicineFrequency)
        FrequencyComboBox.ItemsSource = lst

        lst = dataProvider.GetLookupList(LookupTypes.Periods)
        PeriodComboBox.ItemsSource = lst

        lst = dataProvider.GetLookupList(LookupTypes.GenericDrug)
        lst.Insert(0, New Lookups With {.ID = 0, .Name = "All"})
        GenericDrugComboBox.ItemsSource = lst
        GenericDrugComboBox.SelectedIndex = 0

        lst = dataProvider.GetLookupList(LookupTypes.MedicineManufacturer)
        lst.Insert(0, New Lookups With {.ID = 0, .Name = "All"})
        ManufacturerComboBox.ItemsSource = lst
        ManufacturerComboBox.SelectedIndex = 0

        lst = dataProvider.GetLookupList(LookupTypes.MedicineType)
        lst.Insert(0, New Lookups With {.ID = 0, .Name = "All"})
        MedicineTypeComboBox.ItemsSource = lst
        MedicineTypeComboBox.SelectedIndex = 0

        lstMed = dataProvider.GetMedicineLookupItems()
        MedicineComboBox.ItemsSource = lstMed
        PatientPrescriptionList = New ObservableCollection(Of PatientPrescription)
        PatientPrescriptionDataGrid.ItemsSource = PatientPrescriptionList
    End Sub

    Public Sub BackView() Implements HMSLibrary.IHMSData.BackView

    End Sub

    Public Function DataValidation() As Boolean Implements HMSLibrary.IHMSData.DataValidation

    End Function

    Public Sub FindData() Implements HMSLibrary.IHMSData.FindData

    End Sub

    Public Property ID As Integer Implements HMSLibrary.IHMSData.ID

    Public Sub LoadData(ByVal ID As Integer) Implements HMSLibrary.IHMSData.LoadData

    End Sub

    Public Function SaveData() As Boolean Implements HMSLibrary.IHMSData.SaveData

    End Function

    Private Sub AddPrescriptionButton_Click(ByVal sender As Object, ByVal e As System.Windows.RoutedEventArgs) Handles AddPrescriptionButton.Click
        If Trim(MedicineComboBox.Text) = "" Then
            CommonMessages.ShowValidationMsg("Please select medicine!")
            Exit Sub
        End If
        Dim item As New PatientPrescription
        item.medicineItem = MedicineComboBox.Text
        item.medicineItemID = MedicineComboBox.SelectedValue
        item.dosage = Val(DosageTextBox.Text)
        item.duaration = Val(DurationTextBox.Text)
        item.frequency = FrequencyComboBox.SelectedValue
        item.frequencyName = FrequencyComboBox.Text
        item.periods = PeriodComboBox.Text
        PatientPrescriptionList.Add(item)
    End Sub

    Public Property PatientPrescriptionList As ObservableCollection(Of PatientPrescription)

    Private Sub GenericDrugComboBox_SelectionChanged(ByVal sender As Object, ByVal e As System.Windows.Controls.SelectionChangedEventArgs) Handles GenericDrugComboBox.SelectionChanged
        MedicineFilter()
    End Sub

    Private Sub ManufacturerComboBox_SelectionChanged(ByVal sender As Object, ByVal e As System.Windows.Controls.SelectionChangedEventArgs) Handles ManufacturerComboBox.SelectionChanged
        MedicineFilter()
    End Sub

    Private Sub MedicineTypeComboBox_SelectionChanged(ByVal sender As Object, ByVal e As System.Windows.Controls.SelectionChangedEventArgs) Handles MedicineTypeComboBox.SelectionChanged
        MedicineFilter()
    End Sub

    Private Sub MedicineFilter()
        Dim lst As List(Of MedicineItemsDtl)
        lst = lstMed
        If GenericDrugComboBox.SelectedValue > 0 Then
            lst = (From l In lst Where l.genericDrug = GenericDrugComboBox.SelectedValue Select l).ToList
        End If
        If MedicineTypeComboBox.SelectedValue > 0 Then
            lst = (From l In lst Where l.medicineType = MedicineTypeComboBox.SelectedValue Select l).ToList
        End If
        If ManufacturerComboBox.SelectedValue > 0 Then
            lst = (From l In lst Where l.mfg = ManufacturerComboBox.SelectedValue Select l).ToList
        End If
        MedicineComboBox.ItemsSource = lst
    End Sub

    Private Sub TemplateComboBox_SelectionChanged(ByVal sender As Object, ByVal e As System.Windows.Controls.SelectionChangedEventArgs) Handles TemplateComboBox.SelectionChanged
        If TemplateComboBox.SelectedItem Is Nothing Then Exit Sub
        Dim dataProvider As New MasterDataLayer()
        Dim lst As List(Of PatientPrescription)
        lst = dataProvider.GetPrescriptionTemplateItemsList(TemplateComboBox.SelectedValue)
        For Each l In lst
            PatientPrescriptionList.Add(l)
        Next
    End Sub
End Class
